package core.domain;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class PeerDesciptor implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4650687480728172975L;

	byte[] ipAddress;
	int port;

	public PeerDesciptor() {
	}

	public PeerDesciptor(byte[] ipAddress, int port) {
		super();
		this.ipAddress = ipAddress;
		this.port = port;
	}

	public byte[] getIpAddress() {
		return ipAddress;
	}

	public int getPort() {
		return port;
	}

	public void setIpAddress(byte[] ipAddress) {
		this.ipAddress = ipAddress;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((ipAddress == null) ? 0 : ipAddress.hashCode());
		result = prime * result + port;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PeerDesciptor other = (PeerDesciptor) obj;
		if (ipAddress == null) {
			if (other.ipAddress != null)
				return false;
		} else if (!ipAddress.equals(other.ipAddress))
			return false;
		if (port != other.port)
			return false;
		return true;
	}

	@Override
	public String toString() {
		try {
			return "PeerDesciptor [ipAddress=" + InetAddress.getByAddress(ipAddress).getHostAddress() + ", port=" + port + "]";
		} catch (UnknownHostException e) {
			return "PeerDesciptor [ipAddress=" + ipAddress + ", port=" + port + "]";
		}
	}

}
